{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56d4b4b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "import sys\n",
    "import os\n",
    "import cv2\n",
    "import re\n",
    "\n",
    "sys.path.append(os.path.expanduser('~/Codes/PaddleOCR'))\n",
    "from paddleocr import PaddleOCR\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15cfc39a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def im_show(image):\n",
    "    plt.imshow(image)\n",
    "    plt.show()\n",
    "    \n",
    "def show_txt(frame, det_boxes_scores):\n",
    "    image = frame.copy()\n",
    "    bifen_txt = []\n",
    "    for box, txt_score in det_boxes_scores[0]:\n",
    "        x1, y1 = box[0]\n",
    "        x1, y1 = int(x1), int(y1)\n",
    "        x2, y2 = box[2]\n",
    "        x2, y2 = int(x2), int(y2)\n",
    "        color = (0, 255, 0)\n",
    "        cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)\n",
    "        txt, score = txt_score\n",
    "        txt = txt.replace(' ', '')\n",
    "        match_jie = re.match(r\"第\\d+节\", txt)\n",
    "        match_obj = re.match(r\"\\d+:\\d+\", txt)\n",
    "        if match_obj or match_jie:\n",
    "            bifen_txt.append(txt)\n",
    "            font = cv2.FONT_HERSHEY_SIMPLEX\n",
    "            cv2.putText(image, txt,(x1-10, y1-10),font, 2, (255,0, 0), 3)\n",
    "        if match_obj:\n",
    "            print(\"bifen boxes:\", (x1, y1, x2, y2))\n",
    "            \n",
    "    im_show(image)\n",
    "    return bifen_txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2933ed69",
   "metadata": {},
   "outputs": [],
   "source": [
    "ocr_engine = PaddleOCR()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e24dbdcb",
   "metadata": {},
   "outputs": [],
   "source": [
    "#image_path = '/mnt/data/huangying/datas/比分大屏/现场比分大屏/上半场/live1.ts_imgs/03601.jpeg'\n",
    "#image = cv2.imread(image_path)\n",
    "vid_capture = cv2.VideoCapture('rtmp://192.168.2.138/live')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf8fe532",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "frame_id = 0\n",
    "sucess, frame = vid_capture.read()\n",
    "recog_fps = 25\n",
    "while sucess:\n",
    "    frame_id += 1\n",
    "    sucess, frame = vid_capture.read()\n",
    "    if frame_id % recog_fps == 0:\n",
    "        det_boxes_score = ocr_engine.ocr(frame)\n",
    "        bifen_txt = show_txt(frame, det_boxes_score)\n",
    "        \n",
    "        if(len(bifen_txt) == 2):\n",
    "            zero_box = [425, 245, 575, 350]\n",
    "            x1, y1, x2, y2 = zero_box\n",
    "            number_img = frame[y1:y2, x1:x2, :]\n",
    "            zero_scores = ocr_engine.ocr(number_img, det=False, rec=True, cls=False)\n",
    "            print(\"zero bifen \", zero_scores)\n",
    "        for bifen in bifen_txt:\n",
    "            print(bifen)\n",
    "#boxes, txt_scores, _ = det_boxes_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dae9627d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
